/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.zdmya.action;

import static com.chenzehe.java.core.Constants.PAGE_PATH_404;
import static com.chenzehe.java.core.Status.STATUS_NORMAL;
import static com.chenzehe.zdmya.Constants.SESSION_USER;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.chenzehe.java.core.freemarker.HtmlPathHelp;
import com.chenzehe.java.core.orm.Page;
import com.chenzehe.java.core.orm.PageRequest;
import com.chenzehe.java.core.utils.WebUtil;
import com.chenzehe.zdmya.domain.Area;
import com.chenzehe.zdmya.domain.Category;
import com.chenzehe.zdmya.domain.Collection;
import com.chenzehe.zdmya.domain.Coupon;
import com.chenzehe.zdmya.domain.Fx;
import com.chenzehe.zdmya.domain.Jy;
import com.chenzehe.zdmya.domain.Message;
import com.chenzehe.zdmya.domain.OperatePoint;
import com.chenzehe.zdmya.domain.User;
import com.chenzehe.zdmya.service.IAreaService;
import com.chenzehe.zdmya.service.ICategoryService;
import com.chenzehe.zdmya.service.ICouponService;
import com.chenzehe.zdmya.service.IFxService;
import com.chenzehe.zdmya.service.IJyService;
import com.chenzehe.zdmya.service.IMessageService;
import com.chenzehe.zdmya.service.IUserService;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-7-20 下午11:15:36
 */
@Controller
public class JyAction extends BaseAction {
	@Resource
	IMessageService messageService;
	@Resource
	ICategoryService categoryService;
	@Resource
	IAreaService contentAreaService;
	@Resource
	IFxService fxService;
	@Resource
	ICouponService couponService;
	@Resource
	IJyService jyService;
	@Resource
	IUserService userService;

	@RequestMapping(value = "/jy/index.html")
	public String index(HttpServletRequest request, ModelMap model) {
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 5);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Jy> jys = null;
		String categoryParentEname = WebUtil.getString(request, "c");
		String areaEname = WebUtil.getString(request, "area");
		if (StringUtils.isNotBlank(categoryParentEname)) {
			Category category = categoryService.getByename(categoryParentEname);
			model.addAttribute("category", category);
			jys = jyService.queryJyByCate(pageRequest, categoryParentEname);
		} else if (StringUtils.isNotBlank(areaEname)) {
			Area area = contentAreaService.getAreaByEname(areaEname);
			model.addAttribute("area", area);
			jys = jyService.queryJyByArea(pageRequest, areaEname);
		} else {
			jys = jyService.queryJy(pageRequest);
		}
		model.addAttribute("jys", jys);
		model.addAttribute("currentMenuJy", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitJys = jyService.queryLimitJy(5);
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitJys", limitJys);

		return HtmlPathHelp.getHtmlPathFront("jy");
	}

	@RequestMapping(value = "/jy/detail.html")
	public String detail(HttpServletRequest request, ModelMap model) {
		int id = WebUtil.getInt(request, "id");
		Jy jy = jyService.getById(id);
		if (jy == null || jy.getStatus() != STATUS_NORMAL.getState()) {
			return PAGE_PATH_404;
		}
		jy.setPreJy(jyService.getById(id - 1));
		jy.setNextJy(jyService.getById(id + 1));
		model.addAttribute("jy", jy);
		model.addAttribute("collectionType",
				Collection.CollectionType.JY.type());
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 5);
		PageRequest pageRequestMessage = new PageRequest(pageNo, pageSize);
		pageRequestMessage.setOrderBy("id");
		pageRequestMessage.setOrderDir(PageRequest.Sort.ASC);
		Page<Message> messages = messageService.queryByContentId(
				pageRequestMessage, Message.MessageType.JY.type(), id);
		model.addAttribute("messages", messages);
		model.addAttribute("currentMenuJy", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitHaiTaoJys = jyService.queryLimitJyByArea(5,
				Jy.Area.HTJY.getArea());
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitHaiTaoJys", limitHaiTaoJys);

		return HtmlPathHelp.getHtmlPathFront("jy_detail");
	}

	@RequestMapping("/jy/add_love_ratings.html")
	@ResponseBody
	public String addLoveRatings(HttpServletRequest request) {
		int love_id = WebUtil.getInt(request, "love_id", 0);
		jyService.updatePingFen(love_id);
		User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
		if (user != null) {
			userService.operatePoint(user.getId(), OperatePoint.LIKE);
		}
		return "";
	}

}
